extends CenterContainer


onready var has_double_op = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/has_double/OptionButton
onready var gym_op = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/gym/OptionButton
onready var name_input = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/name/LineEdit
onready var vertical_paint_input = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/vertical_paint/LineEdit
onready var coin_input = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/coin/SpinBox
onready var ip_coin_input = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/ip_coin/SpinBox
onready var start_dialog_input = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/start_dialog/TextEdit
onready var victory_dialog_input = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/victory_dialog/TextEdit
onready var loser_dialog_input = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/loser_dialog/TextEdit
onready var sprite_box = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/spriteBox
onready var fight_use_item_count_input = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/fight_use_item_count/SpinBox
onready var vs_music_input = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/vs_music/LineEdit
onready var vs_music_loop_offset_input = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/vs_music_loop_offset/SpinBox
onready var start_show_time_input = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/StartShowTime/TextEdit
onready var end_show_time_input = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/EndShowTime/TextEdit

onready var dt_op = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/dt/DTSelect
onready var bj_op = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/bj/BJSelect



const NpcFightSpriteItem = preload("res://tool/page/map/mapView/npc/NpcFightSpriteItem.tscn")

var edit_data = null

var npc_data = null


func _ready():
	gym_op.add_item("无", -1)
	gym_op.set_item_id(0, -1)
	
	for gym in DataSource.get_data_list(DataSource.gyms):
		gym_op.add_item(gym["name"], gym["id"])


func set_edit_data(npc_data, edit_data = null):
	self.npc_data = npc_data
	
	self.edit_data = edit_data
	
	if edit_data:
		has_double_op.select(has_double_op.get_item_index(edit_data["hasDouble"]))
		name_input.text = edit_data["name"]
		vertical_paint_input.text = edit_data["verticalPaint"]
		coin_input.value = edit_data["coin"]
		ip_coin_input.value = edit_data.get("ipCoin", 0)
		start_dialog_input.text = edit_data["startDialog"]
		victory_dialog_input.text = edit_data["victoryDialog"]
		loser_dialog_input.text = edit_data["loserDialog"]
		fight_use_item_count_input.value = edit_data["fightUseItemCount"]
		vs_music_input.text = edit_data["vsMusic"]
		vs_music_loop_offset_input.value = edit_data["vsMusicLoopOffset"]
		dt_op.select_value(edit_data["dt"]) 
		bj_op.select_value(edit_data["bj"]) 
		if edit_data.has("startShowTime") and edit_data["startShowTime"] > 0:
			start_show_time_input.text = Time.get_datetime_string_from_unix_time(edit_data["startShowTime"] / 1000,true) 
		if edit_data.has("endShowTime") and edit_data["endShowTime"] > 0:
			end_show_time_input.text = Time.get_datetime_string_from_unix_time(edit_data["endShowTime"] / 1000,true) 
		
		gym_op.select(gym_op.get_item_index(edit_data.get("gymId", -1)))
		
		# 战斗队伍
		var fight_sprite_list = edit_data.get("spriteList", [])
		
		for sprite in fight_sprite_list:
			var item_ui = NpcFightSpriteItem.instance()
			sprite_box.add_child(item_ui)
			item_ui.set_data(sprite, true)
			
		$PanelContainer/VBoxContainer/modalTitle.text = "编辑 %s 【%s】战斗" % [npc_data.get("name", ""), edit_data["name"]]
	else:
		$PanelContainer/VBoxContainer/modalTitle.text = "添加 %s 战斗" % npc_data.get("name", "")
		pass
	
	

func _on_ok_button_up():
	var map_view = MainTool.getUILayer().get_ui("MapView")
	
	var position_info = map_view.get_select_position_info()
	
	if edit_data:
		read_form(edit_data)
		
		if !check_form(edit_data):
			return
		
		DataSource.update_data(DataSource.npc_fight, edit_data)
	else:
		var data = {
			"mapId": position_info["map_register"]["id"],
			"npcId": npc_data["id"]
		}
		read_form(data)
		
		if !check_form(data):
			return
		
		DataSource.add_data(DataSource.npc_fight, data)
	
	MainTool.toast_info("更新战斗配置信息成功!")
	
	map_view.sprite_area_update()
	
	MainTool.getUILayer().pop_ui()


# 读取表单数据到
func read_form(dic:Dictionary):
	dic["hasDouble"] = has_double_op.get_selected_id()
	dic["name"] = name_input.text
	dic["verticalPaint"] = vertical_paint_input.text
	dic["coin"] = coin_input.value
	dic["ipCoin"] = ip_coin_input.value
	dic["startDialog"] = start_dialog_input.text
	dic["victoryDialog"] = victory_dialog_input.text
	dic["loserDialog"] = loser_dialog_input.text
	dic["gymId"] = gym_op.get_selected_id()
	dic["fightUseItemCount"] = fight_use_item_count_input.value
	dic["vsMusic"] =  vs_music_input.text
	dic["vsMusicLoopOffset"] = vs_music_loop_offset_input.value
	dic["dt"] = dt_op.get_value()
	dic["bj"] = bj_op.get_value()
	# npc 显示的时间和隐藏的时间，可以为空
	var temp_s = Time.get_unix_time_from_datetime_string(start_show_time_input.text)
	var temp_e = Time.get_unix_time_from_datetime_string(end_show_time_input.text)
	if temp_s > 0:
		dic["startShowTime"] = temp_s * 1000
	else:
		dic.erase("startShowTime")
	if temp_e > 0:
		dic["endShowTime"] = temp_e * 1000
	else:
		dic.erase("endShowTime") 
	
	var sprite_list = []
	
	for child in sprite_box.get_children():
		sprite_list.append(child.get_sprite_data())
		
	dic["spriteList"] = sprite_list


func check_form(data):
	if !data["name"]:
		MainTool.toast_info("名称不能为空")
		return false
		
	
	if data.get("spriteList").size() == 0:
		MainTool.toast_info("请至少添加一只宝可梦！")
		return false
	
	if data.get("spriteList").size() == 1 && data["hasDouble"]:
		MainTool.toast_info("双打至少添加两只只宝可梦！")
		return false
	
	return true



func _on_cancel_button_up():
	MainTool.getUILayer().pop_ui()


func _on_addSprite_button_up():
	if sprite_box.get_child_count() >=6 :
		MainTool.toast_info("战斗队伍不能超过6只精灵")
		return
		
	var sprite_item = NpcFightSpriteItem.instance()
	sprite_box.add_child(sprite_item)
